package net.cmkj.mine.util;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

public class HttpClient {
	private static final Logger log = LoggerFactory.getLogger(HttpClient.class);

    public static String httpPost(String url,String json,Map<String,String> header) throws Exception{
        log.info("*****************httpPostWithJson*****************");
        log.info("send_url:"+url);
        log.info("send_json:"+json);
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient client = HttpClients.createDefault();
        String respContent = null;

        StringEntity entity = new StringEntity(json,"UTF-8");//解决中文乱码问题
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json;charset=UTF-8");
        httpPost.setEntity(entity);

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(10000).setConnectionRequestTimeout(10000)
                .setSocketTimeout(10000).build();
        httpPost.setConfig(requestConfig);

        if(header != null){
            for (String o : header.keySet()) {
                httpPost.addHeader(o, header.get(o));
            }
        }
        HttpResponse resp = client.execute(httpPost);
        log.info("receive:"+resp.getStatusLine().getStatusCode()+","+respContent);
        log.info("*****************httpPost*****************");
        System.out.println("*****************"+resp.getStatusLine().getStatusCode());
        // if(resp.getStatusLine().getStatusCode() == 200) {
        HttpEntity he = resp.getEntity();
        respContent = EntityUtils.toString(he,"UTF-8");
        Header[] allHeaders = resp.getAllHeaders();
        for (int i = 0; i < allHeaders.length; i++) {
            log.info("allHeader"+i+":"+allHeaders[i].toString());
        }
        log.info("allHeaders:"+allHeaders.toString());
        // }
        return respContent;
    }

    public static String httpPost(String url, List<BasicNameValuePair> params, Map<String,String> header) throws Exception{
        if(params != null) {
            String paramsStr = EntityUtils.toString(new UrlEncodedFormEntity(params, "UTF-8"));
            url = url + "?" + paramsStr;
        }
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient client = HttpClients.createDefault();
        String respContent = null;

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(10000).setConnectionRequestTimeout(10000)
                .setSocketTimeout(10000).build();
        httpPost.setConfig(requestConfig);

        if(header != null){
            for (String o : header.keySet()) {
                httpPost.addHeader(o, header.get(o));
            }
        }
        HttpResponse resp = client.execute(httpPost);
        // if(resp.getStatusLine().getStatusCode() == 200) {
        HttpEntity he = resp.getEntity();
        respContent = EntityUtils.toString(he,"UTF-8");
        //  }
        return respContent;
    }

    public static String httpPost(String url,List<BasicNameValuePair> params,String json,Map<String,String> header) throws Exception{
        if(params != null) {
            String paramsStr = EntityUtils.toString(new UrlEncodedFormEntity(params, "UTF-8"));
            url = url + "?" + paramsStr;
        }
        log.info("*****************httpPostWithJson*****************");
        log.info("send_url:"+url);
        log.info("send_json:"+json);
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient client = HttpClients.createDefault();
        String respContent = null;

        StringEntity entity = new StringEntity(json,"UTF-8");//解决中文乱码问题
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json;charset=UTF-8");
        httpPost.setEntity(entity);

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(10000).setConnectionRequestTimeout(10000)
                .setSocketTimeout(10000).build();
        httpPost.setConfig(requestConfig);

        if(header != null){
            for (String o : header.keySet()) {
                httpPost.addHeader(o, header.get(o));
            }
        }
        HttpResponse resp = client.execute(httpPost);
        log.info("receive:"+resp.getStatusLine().getStatusCode()+","+respContent);
        log.info("*****************httpPost*****************");
        System.out.println("*****************"+resp.getStatusLine().getStatusCode());
        // if(resp.getStatusLine().getStatusCode() == 200) {
        HttpEntity he = resp.getEntity();
        respContent = EntityUtils.toString(he,"UTF-8");
        Header[] allHeaders = resp.getAllHeaders();
        for (int i = 0; i < allHeaders.length; i++) {
            log.info("allHeader"+i+":"+allHeaders[i].toString());
        }
        log.info("allHeaders:"+allHeaders.toString());
        // }
        return respContent;
    }

    public static String httpGet(String url,Map<String,String> header) throws Exception{
		log.info("*****************httpGet*****************");
		log.info("send_url:"+url);
	    HttpGet httpGet = new HttpGet(url);
	    CloseableHttpClient client = HttpClients.createDefault();
	    String respContent = null;

		RequestConfig requestConfig = RequestConfig.custom()
				.setConnectTimeout(10000).setConnectionRequestTimeout(10000)
				.setSocketTimeout(10000).build();
		httpGet.setConfig(requestConfig);

	    if(header != null){
	        for (String o : header.keySet()) {
	        	httpGet.addHeader(o, header.get(o));
	        }
        }
	    HttpResponse resp = client.execute(httpGet);
		log.info("receive:"+resp.getStatusLine().getStatusCode()+","+respContent);
	   // if(resp.getStatusLine().getStatusCode() == 200) {
	        HttpEntity he = resp.getEntity();
	        respContent = EntityUtils.toString(he,"UTF-8");
	  //  }
		log.info("result:"+respContent);
	    return respContent;
	}

    public static String httpGet(String url, String param, Map<String,String> header) throws Exception{
        log.info("*****************httpPostWithJson*****************");
        log.info("send_url:"+url);
        log.info("send_param:"+param);
        url = url + "?" + param;
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpClient client = HttpClients.createDefault();
        String respContent = null;

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(10000).setConnectionRequestTimeout(10000)
                .setSocketTimeout(10000).build();
        httpGet.setConfig(requestConfig);

        if(header != null){
            for (String o : header.keySet()) {
                httpGet.addHeader(o, header.get(o));
            }
        }
        HttpResponse resp = client.execute(httpGet);
        log.info("receive+++++++:"+resp.getStatusLine().getStatusCode()+","+respContent);

        // if(resp.getStatusLine().getStatusCode() == 200) {
        HttpEntity he = resp.getEntity();
        respContent = EntityUtils.toString(he,"UTF-8");
        // }
        return respContent;
    }

    public static String httpGet(String url, List<BasicNameValuePair> params, Map<String,String> header) throws Exception{
        //参数转换为字符串
        if(params != null) {
            String paramsStr = EntityUtils.toString(new UrlEncodedFormEntity(params, "UTF-8"));
            url = url + "?" + paramsStr;
        }
        System.out.println("******:"+url.toString());
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpClient client = HttpClients.createDefault();
        String respContent = null;

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(10000).setConnectionRequestTimeout(10000)
                .setSocketTimeout(10000).build();
        httpGet.setConfig(requestConfig);

        if(header != null){
            for (String o : header.keySet()) {
                httpGet.addHeader(o, header.get(o));
            }
        }
        HttpResponse resp = client.execute(httpGet);
        log.info("receive+++++++:"+resp.getStatusLine().getStatusCode()+","+respContent);

        // if(resp.getStatusLine().getStatusCode() == 200) {
        HttpEntity he = resp.getEntity();
        respContent = EntityUtils.toString(he,"UTF-8");
        // }
        return respContent;
    }

    public static String httpPut(String url,String json,Map<String,String> header) throws Exception{
        HttpPut httpPut = new HttpPut(url);
        CloseableHttpClient client = HttpClients.createDefault();
        String respContent = null;

        StringEntity entity = new StringEntity(json,"UTF-8");//解决中文乱码问题
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPut.setEntity(entity);

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(10000).setConnectionRequestTimeout(10000)
                .setSocketTimeout(10000).build();
        httpPut.setConfig(requestConfig);

        if(header != null){
            for (String o : header.keySet()) {
                httpPut.addHeader(o, header.get(o));
            }
        }
        HttpResponse resp = client.execute(httpPut);
        //if(resp.getStatusLine().getStatusCode() == 200) {
        HttpEntity he = resp.getEntity();
        respContent = EntityUtils.toString(he,"UTF-8");
        // }
        return respContent;
    }

    public static String httpDelete(String url, List<BasicNameValuePair> params, Map<String,String> header) throws Exception{
        String paramsStr = EntityUtils.toString(new UrlEncodedFormEntity(params, "UTF-8"));
        url = url + "?" + paramsStr;
        HttpDelete httpDelete = new HttpDelete(url);
        CloseableHttpClient client = HttpClients.createDefault();
        String respContent = null;
        if(header != null){
            for (String o : header.keySet()) {
                httpDelete.addHeader(o, header.get(o));
            }
        }
        HttpResponse resp = client.execute(httpDelete);
        // if(resp.getStatusLine().getStatusCode() == 200) {
        HttpEntity he = resp.getEntity();
        respContent = EntityUtils.toString(he,"UTF-8");
        //  }
        return respContent;
    }


}
